/*
 * Created by JFormDesigner on Fri Jun 28 09:20:34 CST 2024
 */

package scct.zhang.ui;

import java.awt.event.*;
import scct.zhang.dao.UserDao;
import scct.zhang.model.User;

import java.awt.*;
import javax.swing.*;

/**
 * @author 张
 */
public class DlgUserUpdate extends JDialog {
    int id;
    public DlgUserUpdate(Window owner,int id) {
        super(owner);
        this.id = id;
        initComponents();
        initView();
    }

    private void initView(){
//        根据id查询用户，把用户的信息添加到
        UserDao userDao = new UserDao();
        User user = userDao.getUserById(id);
        txtUser.setText(user.getUsername());
        txtPass.setText(user.getPassword());
        txtAge.setValue(user.getAge());
        txtMobile.setText(user.getMobile());
        if (user.getGender().equals("男")){
            radMale.setSelected(true);
        } else {
            radFemale.setSelected(true);
        }


    }

    private void btnCancle(ActionEvent e) {
        // TODO add your code here
        this.dispose();
    }

    private void btnUpdate(ActionEvent e) {
        // TODO add your code here
        String username = txtUser.getText();
        String password = txtPass.getText();
        String mobile = txtMobile.getText();
        int age = Integer.parseInt(txtAge.getValue().toString());
        String gender = radMale.isDoubleBuffered()?"男":"女";
        //        前端验证

        if (!username.matches("^\\w{3,10}$") || !password.matches("^\\w{3,10}$")){
            JOptionPane.showMessageDialog(this,"请输入合法用户名和密码");
            return;
        }
        if (!mobile.matches("^1[3-9]\\d{9}$")){
            JOptionPane.showMessageDialog(this,"请输入合法手机号码");
            return;
        }
        if (age<10 || age>100){
            JOptionPane.showMessageDialog(this,"请输入有效年龄(10-100)");
            return;
        }
//      验证用户名重复
        UserDao userDao = new UserDao();
        if (userDao.updateUser(id,password,gender,age,mobile)){
            JOptionPane.showMessageDialog(this,"修改成功");
            dispose();
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - zhang
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        txtUser = new JTextField();
        txtPass = new JTextField();
        txtMobile = new JTextField();
        txtAge = new JSpinner();
        radMale = new JRadioButton();
        radFemale = new JRadioButton();
        btnUpdate = new JButton();
        btnCancle = new JButton();

        //======== this ========
        setTitle("\u4fee\u6539\u7528\u6237");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u7528\u6237\u540d");
        contentPane.add(label1);
        label1.setBounds(new Rectangle(new Point(85, 35), label1.getPreferredSize()));

        //---- label2 ----
        label2.setText("\u5bc6\u7801");
        contentPane.add(label2);
        label2.setBounds(85, 85, label2.getPreferredSize().width, 17);

        //---- label3 ----
        label3.setText("\u7535\u8bdd\u53f7\u7801");
        contentPane.add(label3);
        label3.setBounds(85, 135, label3.getPreferredSize().width, 17);

        //---- label4 ----
        label4.setText("\u5e74\u9f84");
        contentPane.add(label4);
        label4.setBounds(85, 180, label4.getPreferredSize().width, 17);

        //---- label5 ----
        label5.setText("\u6027\u522b");
        contentPane.add(label5);
        label5.setBounds(85, 225, label5.getPreferredSize().width, 17);

        //---- txtUser ----
        txtUser.setEditable(false);
        contentPane.add(txtUser);
        txtUser.setBounds(155, 35, 215, txtUser.getPreferredSize().height);
        contentPane.add(txtPass);
        txtPass.setBounds(155, 85, 215, 30);
        contentPane.add(txtMobile);
        txtMobile.setBounds(155, 135, 215, 30);
        contentPane.add(txtAge);
        txtAge.setBounds(155, 175, 215, 30);

        //---- radMale ----
        radMale.setText("\u7537");
        contentPane.add(radMale);
        radMale.setBounds(new Rectangle(new Point(160, 225), radMale.getPreferredSize()));

        //---- radFemale ----
        radFemale.setText("\u5973");
        contentPane.add(radFemale);
        radFemale.setBounds(new Rectangle(new Point(325, 225), radFemale.getPreferredSize()));

        //---- btnUpdate ----
        btnUpdate.setText("\u4fee\u6539");
        btnUpdate.addActionListener(e -> btnUpdate(e));
        contentPane.add(btnUpdate);
        btnUpdate.setBounds(new Rectangle(new Point(140, 265), btnUpdate.getPreferredSize()));

        //---- btnCancle ----
        btnCancle.setText("\u53d6\u6d88");
        btnCancle.addActionListener(e -> btnCancle(e));
        contentPane.add(btnCancle);
        btnCancle.setBounds(new Rectangle(new Point(305, 265), btnCancle.getPreferredSize()));

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(515, 385);
        setLocationRelativeTo(getOwner());

        //---- buttonGroup1 ----
        var buttonGroup1 = new ButtonGroup();
        buttonGroup1.add(radMale);
        buttonGroup1.add(radFemale);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - zhang
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JTextField txtUser;
    private JTextField txtPass;
    private JTextField txtMobile;
    private JSpinner txtAge;
    private JRadioButton radMale;
    private JRadioButton radFemale;
    private JButton btnUpdate;
    private JButton btnCancle;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
